import React, { useRef, useState, useEffect, createRef } from 'react';

// useRef保存的是同一个引用，createRef是最新的引用
class Life extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            a: ''
        };
    }
    componentDidMount() {
        const context = require.context('../../images', true, /\.(png)$/);
        console.log(context);

        // context.forEach(key => {

        // });
    }
    render() {
        return (
            <div onClick={() => this.setState({ a: 'sdfsd' })}>
                father
                <Son a={this.state.a}/>
            </div>
        );
    }
}

class Son extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            a: 'son'
        };
        console.log('come constructer');
    }

    static getDerivedStateFromProps(nextProps, prevState) {
        console.log('come getDerivedStateFromProps');
        if (prevState.a !== nextProps.a) {
            return {
                a: nextProps.a
            };
        }
        return null;
    }
    componentDidMount() {
        console.log('componentDidMount');
    }

    componentDidUpdate(preProps, preState) {
        console.log('componentDidUpdate');
        if (preState.a !== this.state.a) {
            console.log('hahha', this.state);
        }
    }

    shouldComponentUpdate(nextProps, nextState) {
        console.log('shouldComponentUpdate');
        if (nextProps.a !== this.props.a) {
            return true;
        }
    }
    render() {
        return (
            <div>{this.state.a}</div>
        );
    }
}
export default Life;